package com.degon.hsyl.choosefile.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.degon.hsyl.choosefile.R;

public class DialogUtil {

    private static AlertDialog dialog;

    public static void showAlertDialog(Activity activity, String chosenFileCount, boolean cancelableTouchOut, AlertDialogBtnClickListener listener) {
        View view = LayoutInflater.from(activity).inflate(R.layout.center_dialog_layout, null);
        TextView showChosenFileCount = view.findViewById(R.id.dialog_chosen_file_count);
        RelativeLayout cancelButton = view.findViewById(R.id.cancel_button);
        RelativeLayout confirmButton = view.findViewById(R.id.confirm_button);
        showChosenFileCount.setText(chosenFileCount);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.clickNegative();
                dialog.dismiss();
            }
        });
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.clickPositive();
                dialog.dismiss();
            }
        });
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setView(view);
        builder.setCancelable(true);//返回键dismiss

        dialog = builder.create();
        dialog.setCanceledOnTouchOutside(cancelableTouchOut);
        dialog.show();

        //设置宽高
        Window window = dialog.getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            window.setBackgroundDrawableResource(android.R.color.transparent);
            lp.gravity = Gravity.CENTER;
            lp.width = dip2px(activity, 320);
            dialog.getWindow().setAttributes(lp);
        }
    }

    public interface AlertDialogBtnClickListener {
        void clickPositive();

        void clickNegative();
    }

    private static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
